package com.ningxia.water.mapper;

import com.ningxia.water.entity.TsWellMeter;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ningxia.water.vo.in.VolumeVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 *
 * @author hz
 * @since 2021-03-15
 */
public interface TsWellMeterMapper extends BaseMapper<TsWellMeter> {


    /**
     * 查询该机井id下是否有设备
     * @return
     */
    @Select("select count(1) from ts_well_meter where well_id=#{wellId}")
    Integer findWellMeters();

    /**
     * 通过设备id删除机井与设备关联关系
     * @param meterId
     * @return
     */
    @Delete("delete from ts_well_meter where meter_id = #{meterId}")
    int deleteByMeterId(Integer meterId);


    /**
     * 统计所有机井今天的所有数据
     * @param startTime
     * @param endTime
     * @return
     */
    @Select("SELECT\n" +
            "\twell_id,\n" +
            "\tsum( instan_flow )/10000 as volume\n" +
            "FROM\n" +
            "\tts_well_meter wm\n" +
            "\tLEFT JOIN ts_meter m ON wm.meter_id = m.meter_id \n" +
            "\tleft join ts_meter_flow mf on mf.meter_id = m.meter_code\n" +
            "WHERE\n" +
            "\tread_time BETWEEN #{startTime} \n" +
            "\tAND #{endTime} \n" +
            "GROUP BY\n" +
            "\twm.well_id")
    List<VolumeVo> groupDayVolume(String startTime, String endTime);

    /**
     * 统计所有机井本月的数据
     * @param dayTime
     * @return
     */
    @Select("SELECT\n" +
            "\twm.well_id,\n" +
            "\tsum( dd.day_data )/10000 as volume\n" +
            "FROM\n" +
            "\tts_well_meter wm\n" +
            "\tLEFT JOIN ts_day_data dd ON dd.well_id = wm.well_id\n" +
            "WHERE\n" +
            "\tdd.time = #{dayTime}\n" +
            "GROUP BY\n" +
            "\twm.well_id")
    List<VolumeVo> groupMonthVolume(String dayTime);
}
